#ifndef __PNSESSIONACTION_H__
#define __PNSESSIONACTION_H__

/**
*@file       pnsessionaction.h
*@brief      session performer
*
*@author     graywind
*@version    1.0.0.0
*@data       2012/03/17
*/

#include <queue>
#include <vector>
#include "pnsession.h"
#include "pnactionbase.h"
#include "boost/thread.hpp"

///net service relative
namespace pnNetlib
{
	///@brief     manage the session that accepted
	class pnSessionAction : public pnActionBase
	{
	private:
		///@brief                           whether accept msg
		bool                                m_bAccept;
		///@brief                           acceptor
		boost::asio::ip::tcp::acceptor*     m_pAcceptor;
		///@brief                           accept thread
		boost::thread*                      m_listenThread;

	private:
		///@breif  the function that running at the listen thread
		void       accept_work(MsgProc msgProc);
		///@brief  accept connection
		void       accept(pnSession::SessionPtr session, MsgProc msgProc);

	public:
		///@breif  constructor
		pnSessionAction();
		///@brief  destructor
		~pnSessionAction();

	public:
		///@brief      initialize this aciton
		///param[in]   ioService     reference of io service object
		///param[in]   max_session   max count of session
		///param[in]   buffer_length bytes length of buffer
		///retval      bool
		bool init(boost::asio::io_service& ioService, int max_session, long buffer_length);
		///@brief      start listening
		bool start_listen(unsigned short port, MsgProc msgProc);
        ///@brief      start connect to server
		///param[in]   ip address of the server
		///param[in]   port of the server
		///param[in]   ptr of callback function
		///retval      int(return -1 if failed)
		int start_new_connection(const std::string& ip, unsigned short port, MsgProc msgProc);
		///@brief      disconnect a session
        ///param[in]   session_index     index in the array
        ///retval      void
		void close_session(int session_index);
        ///@brief      disconnect all session
        ///retval      void
        void close_all_session();
		///@brief      teminate acceptor stuff
        ///retval      void
		void close_listen();
	};
};

#endif